package pl.kosmowska.ewelina.client.views.impl;

import java.util.List;

import pl.kosmowska.ewelina.client.ViewMessages;
import pl.kosmowska.ewelina.client.app.requestfactory.dto.LoanDataProxy;
import pl.kosmowska.ewelina.client.views.LoanView;
import pl.kosmowska.ewelina.client.views.widgets.MessageDialogBox;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanDialog;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEvent;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEvent.Selected;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEventHandler;
import pl.kosmowska.ewelina.client.views.widgets.grids.LoanDataGrid;
import pl.kosmowska.ewelina.client.views.widgets.popup.PopupMessage;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.ResizeLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

public class LoanViewImpl extends Composite implements LoanView{

	private static LoanViewImplUiBinder uiBinder = GWT
			.create(LoanViewImplUiBinder.class);

	@UiField DockLayoutPanel loanLayout;
	@UiField ResizeLayoutPanel loanPanel;
	@UiField Button addLoan;
	@UiField Button editLoan;
	@UiField Button deleteLoan;
	private BooleanDialog wconfirm;


	interface LoanViewImplUiBinder extends UiBinder<Widget, LoanViewImpl> {
	}

	private Presenter presenter;
	private boolean alreadyInit;


	private LoanDataGrid<LoanDataProxy> loanGrid;

	public LoanViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	public void reInitPagingDataGrid(){
		loanPanel.clear();
		loanGrid = new LoanDataGrid<LoanDataProxy>();
		loanGrid.setHeight("600px");
		loanGrid.clear();
		loanPanel.add(loanGrid);
	}
	
	@Override
	public void reInitView(List<LoanDataProxy> loanDataList) {
		System.out.println("LoanViewImpl.setLoggedIn(): Loading Loans");
		reInitPagingDataGrid();
		if (loanDataList == null || loanDataList.size() == 0) {
			loanGrid.setEmptyTableMessage(ViewMessages.NO_DATA_TO_SHOW);
			showMessage(ViewMessages.NO_DATA_TO_SHOW);
		}else{
			loanGrid.setDataList(loanDataList);	
		}
	} 


	@UiHandler("deleteLoan")
	void onDeleteLoanClick(ClickEvent event) {
		if(loanGrid.getSelectedItem()!=null){
			shownDeleteConfirmationDialog();
		}else{
			showMessage(ViewMessages.NOTHING_SELECTED_TO_DELETE);
		}

	}
	@UiHandler("editLoan")
	void onEditLoanClick(ClickEvent event) {
		if(loanGrid.getSelectedItem()!=null){
			presenter.goToLoanEditPlace(loanGrid.getSelectedItem());
		}else{
			showMessage(ViewMessages.NOTHING_SELECTED_TO_EDIT);
		}

	}
	@UiHandler("addLoan")
	void onAddLoanClick(ClickEvent event) {
		presenter.goToLoanAddPlace();
	}

	private void shownDeleteConfirmationDialog() {
		if (wconfirm == null) {
			wconfirm = new BooleanDialog(ViewMessages.CONFIRM_DELETE);
			wconfirm.addSelectionHandler(new BooleanEventHandler() {
				public void onBooleanEvent(BooleanEvent event) {
					if (event.getBooleanEvent() == Selected.TRUE) {
						presenter.deleteLoanData(loanGrid.getSelectedItem());
					} else if (event.getBooleanEvent() == Selected.FALSE) {
						// do nothing
					}
				}
			});
		}
		wconfirm.center();
	}

	@Override
	public void showMessage(String errorMessage) {
//		MessageDialogBox messageDialogBox = new MessageDialogBox(errorMessage);
//		messageDialogBox.center();
//		messageDialogBox.show();
        final PopupMessage alertMessage = new PopupMessage(errorMessage);
        alertMessage.startProcessing();
        Timer t = new Timer() {
            public void run() {
                alertMessage.stopProcessing();
            }
        };
        t.schedule(1000);

	}

	@Override
	public void updateAfterLoanDelete(LoanDataProxy selectedItem) {
		loanGrid.removeFromDataList(selectedItem);
	} 
	
	@Override
	public boolean isAlreadyInit() {
		return alreadyInit;
	}

	@Override
	public void setAlreadyInit(boolean alreadyInit) {
		this.alreadyInit = alreadyInit;
	}
	

}


